package com.renwei.yunlong.activity.work;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.CommonSearchActivity;
import com.renwei.yunlong.adapter.SpareSelectAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.SpareSearchBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.qrcode.SimpleCaptureActivity;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SearchImage;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择备件对象--页面
 * <p>
 * Created by Administrator on 2017/8/29.
 */

public class SpareSelectActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.search_image)
    SearchImage searchImage;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    private SpareSelectAdapter adapter;
    private String searchName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_object);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        initView();
        initData();
    }

    private void initView() {
        searchImage.setOnClickListener(this);
       // simpleTileView.setRightOption(R.mipmap.scroll, this);

        simpleTileView.setTitle("选择备件对象");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SpareSelectAdapter(SpareSelectActivity.this);
        adapter.setOnItemClickLitener(new BaseRecyclerViewAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                SpareSearchBean.Rows item = adapter.getItem(position);
                Intent intent = new Intent();
                intent.putExtra("hardName", item.getAssetName());
                intent.putExtra("spareId", item.getStoreId());
                setResult(RESULT_OK, intent);
                SpareSelectActivity.this.finish();
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
        } else if ("2".equals(companyType)) {
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        map.put("searchName", searchName);
        ServiceRequestManager.getManager().getSpareSelectList(this, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 520) {
                setResult(RESULT_OK, data);
                finish();
            }
        } else if (requestCode == 1002) {
           showCenterInfoMsg("");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_parent:
                Intent intent = new Intent(this, SimpleCaptureActivity.class);
                startActivityForResult(intent, 1002);
                break;
            case R.id.search_image:
                CommonSearchActivity.OpenActivityFromSpare(this, v, SpareSelectActivity.class, 0);
                break;
                default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                SpareSearchBean bean = new Gson().fromJson(result, SpareSearchBean.class);
                if (bean.getMessage().getCode() == 200) {
                    if (bean.getRows() != null && bean.getRows().size() != 0) {
                        stateLayout.showContentView();
                        adapter.setData(bean.getRows());
                    } else {
                        stateLayout.showEmptyView();
                    }
                } else {
                    showCenterInfoMsg(bean.getMessage().getMessage());
                }
                break;
                default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
